package com.example.demo.poc.example;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import net.minidev.json.JSONArray;
import org.apache.commons.io.FileUtils;
import org.apache.commons.text.StringEscapeUtils;

import java.io.File;
import java.io.IOException;
import java.util.LinkedHashMap;

public class JayWayJsonpathMapTest {

    public static void main(String[] args) {
        File f = new File("C:\\Users\\shink\\OneDrive\\DI\\design\\doc\\SZTXDB-QT0000-0039.json");
        String t = null;
        try {
            t = FileUtils.readFileToString(f, "UTF8");
            t = StringEscapeUtils.unescapeJson(t);
            t=t.replaceAll("\"\\{","{");
            t=t.replaceAll("}\"","}");

        } catch (IOException e) {
            e.printStackTrace();
        }

//        System.out.println(t);


        DocumentContext context = JsonPath.parse(t);
        Object res = context.read("$.message.nativePayload[0].sourceMetadata.tags[*]['key','value']");
//        Object res = context.read("$.message.nativePayload[0].sourceMetadata.tags[*].key");
//        Object res = context.read("$.message.nativePayload[0].createTime");
//        Object res = context.read("$.message.nativePayload[0].sourceMetadata.tags[?(@.key == 'location')].value");
        System.out.println(res);
        System.out.println(res.getClass());
        if(res instanceof JSONArray){
            JSONArray _arr = (JSONArray) res;
            for (int i = 0; i < _arr.size(); i++) {
                Object o = _arr.get(i);
                System.out.println(o);
                System.out.println(o.getClass());
                if(o instanceof LinkedHashMap) {
                    LinkedHashMap<String, Object> _lmap = (LinkedHashMap<String, Object>) o;
                }
            }
        }
    }
}
